package controlador;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import modelo.BibliotecaModel;
import modelo.Player;
import modelo.Partida;
import modelo.Player.Colors;
import modelo.ScriptoriumModel;
import cards.Biblioteca;
import cards.Card;
import cards.Cripta;
import cards.Features;
import cards.Scriptorium;
import modelo.Constants;
import tablero.Square;
import vista.Vista;

public class GameController {
	private Partida partida;
	private Vista vista;
	private List<Card> deskCards;
	
	
	
	public GameController(Partida partida) {
		this.partida = partida;
		deskCards = new LinkedList<Card>();
		abadiaDeskCardsInit();
	}

	public void setVista(Vista vista) {
		this.vista = vista;
	}

	public boolean movePlayer(Player j, Square c) {
		if (partida.playerMove(j, c))
			return true;
		else
			return false;
	}

	public boolean movePlayer(Player j, int casilla) {
		Square c = partida.getSquare(casilla);
		if (partida.playerMove(j, c))
			return true;
		else
			return false;
	}

	public void moverACasillaInicial() {
		partida.moverACasillaInicial();
		
	}

	public void addPlayer(String nombre, int color) {
		color--;
		Colors[] colores = Colors.values();
		Colors eleccion = colores[color];
		partida.addPlayer(nombre, eleccion);
	}

	public int askQuestion(Player j, int where, List<Features> questions) {
		return partida.askQuestion(j, where, questions);
	}

	public boolean askQuestion(Player j1, Player j2, int where, String name) {
		return partida.askQuestion(j1, j2, where, name);
	}

	public void writeDown(Player j, int id, String annotation) {
		partida.writeDown(j, id, annotation);
	}

	public void empezar() {
		partida.setEmpezada();
		partida.buildAbadiaMap();
		partida.inicializarPartida();
		partida.turnos();
	}

	// JOSE
	public LinkedList<Card> getDeskCards() {
		return (LinkedList<Card>) deskCards;
	}

	public void setDeskCards(LinkedList<Card> deskCards) {
		this.deskCards = deskCards;
	}


	public void abadiaDeskCardsInit() {
		//ReadFromXML reader = new ReadFromXML(Constants.MAP_FILE);
//		for (int i=0; i<((Biblioteca)biblioteca).getLibraryCards().size();i++)
//			deskCards.add(((Biblioteca)biblioteca).getLibraryCards().get(i));
//	//	for(Biblioteca pibot:((Biblioteca)biblioteca).getLibraryCards())
//		//((Biblioteca)biblioteca).getLibraryCards())
//			//add(((Biblioteca)biblioteca).getLibraryCards().get(pibot.ordinal()));
//			deskCards.add(biblioteca.)
//		
//	
//		
		
	}
//TODO:cargar d
	private class ReadFromXML extends DefaultHandler {

		private SAXParserFactory spfac;
		SAXParser sp;
		Square sqrTmp;
		String tmpValue;
		Square origen;
		ArrayList<String> cardsImageDirectory;

		public ReadFromXML(String fichero) {
			 spfac = SAXParserFactory.newInstance();
			 try {
			 sp = spfac.newSAXParser();
			 } catch (ParserConfigurationException e) {
			 // TODO Auto-generated catch block
			 e.printStackTrace();
			 } catch (SAXException e) {
			 // TODO Auto-generated catch block
			 e.printStackTrace();
			 }
			 try {
			 sp.parse(fichero, this);
			 } catch (SAXException e) {
			 // TODO Auto-generated catch block
			 e.printStackTrace();
			 } catch (IOException e) {
			 // TODO Auto-generated catch block
			 e.printStackTrace();
			 }
		}

		// public void startElement(String uri, String localName)
		// String elementName, Attributes attributes) throws SAXException {
		// if(elementName.equalsIgnoreCase("square")){
		// sqrTmp = new Square();
		// }
	}

	public void endElement(String s, String s1, String element)
			throws SAXException {
		// if(element.equalsIgnoreCase("square")){
		// addSquare(sqrTmp);
		// }
		// if(element.equalsIgnoreCase("name")){
		// sqrTmp.setSquareName(tmpValue);
		// }
		// if(element.equalsIgnoreCase("number")){
		// sqrTmp.setSquareNumber(Integer.parseInt(tmpValue));
		// }
		// if(element.equalsIgnoreCase("origin")){
		//
		// }
	}

//	public void deskCardOption(String location) {
//		switch(location.toLowerCase())
//		{
//		case "biblioteca": vista.giveCardtoUser(getBiblioCards().get(getBiblioCards().size()-1));
//		}
//		
//	}

	// public void characters(char[] ac, int i, int j) throws SAXException {
	// tmpValue = new String(ac, i, j);
	// }
}// END PRIVATE CLASS

// }
